﻿using MassTransit;
using MassTransit.NewIdProviders;
using SqlSugar;
using System;
using System.Runtime.InteropServices;

namespace WucCloud.Utility
{
    /// <summary>
    /// Guid帮助类
    /// </summary>
    public class IDGenerator
    {
        #region 生成无序的Guid
        [System.Runtime.InteropServices.DllImport("Rpcrt4", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
        private static extern long UuidCreateSequential(ref System.Guid ptrGuid);

        /// <summary>
        /// 生成无序的GUID
        /// </summary>
        /// <returns></returns>
        public static Guid CreateGuid()
        {
            Guid id = Guid.Empty;
            long num = IDGenerator.UuidCreateSequential(ref id);
            if (0L != num)
            {
                return Guid.NewGuid();
            }
            return id;
        }
        #endregion

        #region 生成顺序的Guid
        /// <summary>
        /// 生成顺序的Guid
        /// </summary>
        /// <returns></returns>
        public static string CreateNewId()
        {
            //设置生成标识符时包含processId（进程Id），保证运行在同一台机器上的多个进程生成的标识符不会重复
            NewId.SetProcessIdProvider(new CurrentProcessIdProvider());
            //生成Guid
            var guid = NewId.NextGuid();
            return guid.ToString();
        }
        #endregion

        #region 生成雪花Id
        /// <summary>
        /// 生成雪花Id
        /// </summary>
        /// <returns></returns>
        public static string SnowflakeId()
        { 
            var snowyId = SnowFlakeSingle.instance.getID();
            return snowyId.ToString();
        }
        #endregion
    }
}
